package com.ccc.boot.monitor.service.impl;

import cn.hutool.json.JSONUtil;
import com.ccc.boot.monitor.pojo.RedisVO;
import com.ccc.boot.monitor.service.CacheService;
import com.ccc.boot.redis.constant.RedisConstant;
import com.ccc.boot.redis.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author superC
 * @version 2.0.0
 * @date 2022/4/24
 */
@Service
public class CacheServiceImpl implements CacheService {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public boolean delete(String key) {
        redisUtils.del(key);
        return true;
    }

    @Override
    public boolean deleteByPrefix(String prefix) {
        redisUtils.delByPrefix(prefix);
        return true;
    }

    @Override
    public boolean flushRedis() {
        redisUtils.flushDb();
        return true;
    }

    @Override
    public List<RedisVO> getList() {
        List<RedisVO> list = new ArrayList<>();
        // 获取所有key
        Set<String> keys = redisUtils.keys(RedisConstant.BASE_PRE);
        // 遍历获取内容
        for (String key : keys) {
            if (StringUtils.isEmpty(key)) {
                continue;
            }
            // 过滤掉权限的缓存
            if (key.contains(RedisConstant.AUTH)) {
                continue;
            }

            String value = getValueByKey(key);

            RedisVO vo = RedisVO.builder().key(key).value(value).build();
            list.add(vo);
        }
        return list;
    }

    /**
     * 根据key，获取其所对应的json格式的值
     *
     * @param key
     * @return {@link String}
     * @date 2022/4/25 15:16
     * @author superC
     */
    private String getValueByKey(String key) {
        String value = null;
        DataType dataType = redisUtils.type(key);
        switch (dataType.code()) {
            case "string":
                value = redisUtils.get(key);
                break;
            case "list":
                List<String> list = redisUtils.lGet(key);
                if (!list.isEmpty()) {
                    value = JSONUtil.toJsonStr(list);
                }
                break;
            case "set":
                Set<String> set = redisUtils.sGet(key);
                if (!set.isEmpty()) {
                    value = JSONUtil.toJsonStr(set);
                }
                break;
            case "zset":
                Set<String> zset = redisUtils.zGet(key);
                if (!zset.isEmpty()) {
                    value = JSONUtil.toJsonStr(zset);
                }
                break;
            case "hash":
                Map<String, Object> map = redisUtils.hmGet(key);
                if (!map.isEmpty()) {
                    value = JSONUtil.toJsonStr(map);
                }
                break;
            default:
                break;
        }

        return value;
    }
}
